package com.example.demo.controller;


import com.example.demo.entity.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    // POST /api/students
    // 注册/创建学生
    @PostMapping
    public ResponseEntity<?> createStudent(@RequestBody Student student) {
        try {
            Student newStudent = studentService.createStudent(student);
            // 实际返回时，应确保敏感信息（如密码）被移除或使用 DTO
            newStudent.setPassword(null);
            return new ResponseEntity<>(newStudent, HttpStatus.CREATED);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage()); // 400 Bad Request
        } catch (IllegalStateException e) {
            return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getMessage()); // 409 Conflict
        } catch (RuntimeException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("注册失败: " + e.getMessage());
        }
    }

    // GET /api/students
    // 查询所有学生
    @GetMapping
    public List<Student> getAllStudents() {
        List<Student> students = studentService.findAllStudents();
        // 实际应用中，这里应该使用 DTO 移除 password 字段
        students.forEach(s -> s.setPassword(null));
        return students;
    }

    // GET /api/students/{id}
    // 根据学号查询单个学生
    @GetMapping("/{id}")
    public ResponseEntity<Student> getStudentById(@PathVariable("id") String studentId) {
        return studentService.findStudentById(studentId)
                .map(student -> {
                    student.setPassword(null); // 返回前移除密码
                    return ResponseEntity.ok(student);
                })
                .orElse(ResponseEntity.notFound().build()); // 404 Not Found
    }

    // GET /api/students/class/{className}
    // 根据班级查询学生
    @GetMapping("/class/{className}")
    public List<Student> getStudentsByClass(@PathVariable("className") String className) {
        List<Student> students = studentService.findStudentsByClassName(className);
        students.forEach(s -> s.setPassword(null));
        return students;
    }

    // PUT /api/students/{id}
    // 更新学生信息
    @PutMapping("/{id}")
    public ResponseEntity<?> updateStudent(@PathVariable("id") String studentId, @RequestBody Student studentDetails) {
        studentDetails.setStudentId(studentId); // 确保更新的是路径中的 ID

        if (studentService.updateStudent(studentDetails)) {
            return ResponseEntity.ok("学生信息更新成功");
        } else {
            return ResponseEntity.notFound().build(); // 404 Not Found (原学生不存在)
        }
    }

    // DELETE /api/students/{id}
    // 删除学生
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteStudent(@PathVariable("id") String studentId) {
        if (studentService.deleteStudent(studentId)) {
            return ResponseEntity.noContent().build(); // 204 No Content
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}